/* shttpd - send status reports from the worker to the control thread
   Copyright (C) 2018 Ariadne Devos

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#include <sHT/intrisics.h>
#include "worker.h"

#include <errno.h>
#include <stdint.h>
#include <sys/epoll.h>
#include <sys/socket.h>

enum sHT_status_id {
	/* typically about resource usage*/
	sHT_STATUS_CHANGE_FLAGS = 0,
};

__attribute__((nonnull (1, 2)))
static void
sHT_sendstatus_flags(struct sHT_worker *worker, struct sHT_task *task, sHT_worker_flags flags)
{
	sHT_assert(flags <= 0xffff);
	/* If this turns out to benefit from optimisation,
	   consider letting the type be uint32_t,
	   while maintaining this byte order. */
	const unsigned char msg[4] = {
		(sHT_STATUS_CHANGE_FLAGS & 0xff00) >> 8,
		(sHT_STATUS_CHANGE_FLAGS & 0x00ff) >> 0,
		(flags & 0xff00) >> 8,
		(flags & 0x00ff) >> 0,
	};
	int ret;
	do ret = send(task->fd, msg, sizeof(msg), MSG_DONTWAIT | MSG_NOSIGNAL);
	while (ret)
}

void
sHT_sendstatus_task(struct sHT_worker *worker, struct sHT_task *task)
{
	if (!(task->epollflags & EPOLLOUT))
		return;
	if (worker->flags)
		sHT_sendstatus_flags(worker, task, worker->flags)
}
